// pages/detail/detail.js
var WxParse = require('../../wxParse/wxParse.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgs: [
            "../../image/goods01.jpg",
            "../../image/goods02.jpg",
            "../../image/goods03.jpg",
            "../../image/goods04.jpg"
        ],
        boolen: true,
        lists: {



        },
        num: 0,
        indicatorDots: true, //是否显示面板指示点
        autoplay: true, //是否自动播放
        interval: 2000, //停留时间间隔
        duration: 1000, //播放时长
        previousMargin: '50px', //前边距，可用于露出前一项的一小部分，接受 px 和 rpx 值
        nextMargin: '50px', //后边距，可用于露出后一项的一小部分，接受 px 和 rpx 值
        circular: true, //是否采用衔接滑动
        currentSwiperIndex: 0, //swiper当前索引


    },
    gocart() {
        wx.switchTab({
            url: "../cart/cart"
        })
    },
    gohome() {
        wx.switchTab({
            url: "../home/home"
        })
    },
    goplay() {
        wx.navigateTo({
            url: "../play/play"
        })
    },
    getCap() {
        let objectAll = {
            id: this.data.lists.id,
            goods: this.data.lists.goodsImage,
            name: this.data.lists.goodsName,
            price: this.data.lists.goodsPrice,
            address: "广州",
            num: 1,
            boolen:false,
        };
        let storageAll = wx.getStorageSync('GoodsCarList');
        console.log(typeof storageAll);
        if (storageAll) {
            console.log(123)
            let index = storageAll.findIndex(item => {
                return item.id == this.data.lists.id
            });
            if (index != -1) {
                storageAll[index].num += 1;
            } else {
                storageAll.push(objectAll);
            }
            wx.setStorageSync('GoodsCarList', storageAll);

        } else {
            wx.setStorageSync('GoodsCarList', [objectAll]);
        }
        this.setData({
            num: wx.getStorageSync('GoodsCarList').length,
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        this.getGoodsData(options.id);
        console.log(  this.getGoodsData(options.id));

    },
    swiperBindchange(e) {
        this.setData({
            currentSwiperIndex: e.detail.current
        })
    },
    getGoodsData(id) {
        let _this = this;
        console.log(123);
        console.log(id);
        wx.request({
            url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/detail', //仅为示例，并非真实的接口地址
            data: {
                goods_id: id
            },
            header: {
                'content-type': 'application/json' // 默认值
            },
            success(res) {
                console.log(res)
                let {
                    goods_id,
                    goods_name,
                    goods_small_logo,
                    goods_price,
                    pics,
                    goods_introduce
                } = res.data.message;
                let data = {
                    id: goods_id,
                    goodsName: goods_name,
                    goodsImage: goods_small_logo,
                    img: pics,
                    goodsPrice: goods_price,
                    goodsPriceOld: goods_price + 1000,
                    goodsDetail: goods_introduce


                }
                WxParse.wxParse("goods_introduce", "html",
                    goods_introduce,
                    _this, 5
                );
                _this.setData({
                    lists: data
                })
            }
        })
    },
    changeColor() {
        this.setData({
            boolen:!this.data.boolen
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            num: wx.getStorageSync('GoodsCarList').length || 0,
        })

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})